package com.batchaps.classes;

import java.util.Random;

import javax.microedition.lcdui.Graphics;

import com.batchaps.global.GameEngine;

public class Enemy2 {
	private int iWidth , iHeight , iX , iY , timeFire;
	private Sprite2D Enemy;
	private Sprite2D[] Bullet1 , Bullet2;
	private int[] isShoot1 , isShoot2;
	private int health , havePower , isDeath , nBullet1 , nBullet2;
	private boolean checkDeath;
	private int[] turnFire2;
	
	public Enemy2(String strEnemy , int nEnemy , String strType , int width , int height){
		iWidth = width;
		iHeight = height;
		health = GameEngine.Health;
		isDeath = GameEngine.IsDeath;
		
		nBullet1 = 6;
		Bullet1 = new Sprite2D[nBullet1];
		isShoot1 = new int[nBullet1];
		
		nBullet2 = 2;
		Bullet2 = new Sprite2D[nBullet2];
		isShoot2 = new int[nBullet2];
		
		checkDeath = false;
		turnFire2 = new int[nBullet1];
		
		for(int i = 0 ; i < nBullet1 ; i++){
			turnFire2[i] = 0;
		}
		//load image
		Enemy = new Sprite2D(strEnemy, nEnemy, strType, width, height, GameEngine.EnemySize, GameEngine.EnemySize, 0, 0);
		Enemy.set_visible(false);
		
		//load bullet
		for(int i = 0 ; i < nBullet1 ;i++){
			Sprite2D bullet = new Sprite2D("",5,".png");
			Bullet1[i] = bullet;
			Bullet1[i].set_visible(false);
			isShoot1[i] = 0;
		}
		for(int i = 0 ; i < nBullet2 ;i++){
			//
		}
		
		iX = (int)Enemy.get_posX();
		iY = (int)Enemy.get_posY();
	}
	
	public void Draw(Graphics g){
		Enemy.Draw(g);
		for(int i = 0 ; i < nBullet1 ;i++){
			Bullet1[i].Draw(g);
		}
		for(int i = 0 ; i < nBullet2 ;i++){
			Bullet2[i].Draw(g);
		}
	}
	
	public void enemyFireType1(int scrY){
		timeFire--;
		iX = (int)Enemy.get_posX();
		iY = (int)Enemy.get_posY();
		for(int i = 0 ; i < nBullet1 ;i++){
			if(Bullet1[i].getY() > 0 + iHeight){
				Bullet1[i].set_visible(false);
				isShoot1[i] = 0;
			}
		}
		for (int i = 0; i < nBullet1; i++)
        {
            if (isShoot1[i] != 0)
            {
                switch (isShoot1[i])
                {
                    case 1: Bullet1[i].Move(0, 5); break;
                    case 2: Bullet1[i].Move(-1, 5); break;
                    case 3: Bullet1[i].Move(1, 5); break;
                }
            }
        }
		if (timeFire <= 0 && 0 < Enemy.getY() + Enemy.get_height() / 2 && !checkDeath)
        {
            for (int i = 0; i < nBullet1; i++)
            {
                if (isShoot1[i] == 0)
                {
                    Bullet1[i].setPosition(iX, iY);
                    Bullet1[i].set_visible(true);
                    isShoot1[i] = 1;
                    for (int j = i + 1; j < nBullet1; j++)
                    {
                        if (isShoot1[j] == 0)
                        {
                            Bullet1[j].setPosition(iX, iY);
                            Bullet1[j].set_visible(true);
                            isShoot1[j] = 2;
                            for (int k = j + 1; k < nBullet1; k++)
                            {
                                if (isShoot1[k] == 0)
                                {
                                    Bullet1[k].setPosition(iX, iY);
                                    Bullet1[k].set_visible(true);
                                    isShoot1[k] = 3;
                                    timeFire = 70;
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
	}
	
	public void enemyFireType2(int scrY){
		timeFire--;
		iX = (int)Enemy.getX();
		iY = (int)Enemy.getY();
		for (int i = 0; i < nBullet1; i++)
        {
            if (Bullet1[i].getY() > 0 + iHeight)
            {
                Bullet1[i].set_visible(false);
                isShoot1[i] = 0;
            }
        }
        for (int i = 0; i < nBullet1; i++)
        {
            if (Bullet1[i].getX() < iX - 50)
            {
                turnFire2[i] = 1;
            }
            if (Bullet1[i].getX() > iX + 50)
            {
                turnFire2[i] = 0;
            }
            if (isShoot1[i] != 0)
            {
                switch (turnFire2[i])
                {
                    case 0:
                        {
                            Bullet1[i].Move(-4, 7);
                        } break;
                    case 1:
                        {
                            Bullet1[i].Move(4, 7);
                        } break;
                }
            }
        }
        if (timeFire <= 0 && 0 < Enemy.getY() + Enemy.get_height() / 2 && !checkDeath)
        {
            for (int i = 0; i < nBullet1; i++)
            {
                if (isShoot1[i] == 0)
                {
                    Bullet1[i].setPosition(iX, iY);
                    Bullet1[i].set_visible(true);
                    isShoot1[i] = 1;
                    timeFire = 20;
                    break;
                }
            }
        }
	}
	
	public void enemyFireType3(Sprite2D player , int scrY){
		timeFire--;
        iX = (int)Enemy.getX();
        iY = (int)Enemy.getY();
        for (int i = 0; i < nBullet2; i++)
        {
            if (Bullet2[i].getY() > 0 + iHeight)
            {
                Bullet2[i].set_visible(false);
                isShoot2[i] = 0;
            }
        }
        for (int i = 0; i < nBullet2; i++)
        {
            if (isShoot2[i] != 0)
            {
                if (Bullet2[i].getX() == player.getX())
                {
                    Bullet2[i].Move(0, 5);
                }
                if (Bullet2[i].getX() < player.getX())
                {
                    Bullet2[i].Move(3, 5);
                }
                if (Bullet2[i].getX() > player.getX())
                {
                    Bullet2[i].Move(-3, 5);
                }
                Bullet2[i].NextFrame();
            }
        }
        if (timeFire <= 0 && 0 < Enemy.getY() + Enemy.get_height() / 2 && !checkDeath)
        {
            for (int i = 0; i < nBullet2; i++)
            {
                if (isShoot2[i] == 0)
                {
                    Bullet2[i].setPosition(iX, iY);
                    Bullet2[i].set_visible(true);
                    isShoot2[i] = 1;
                    timeFire = 70;
                    break;
                }
            }
        }
	}
	
	public int checkFire(Sprite2D sprite, int health)
    {
        for (int i = 0; i < nBullet1; i++)
        {
            if (Bullet1[i].CollidesWith(sprite))
            {
                Bullet1[i].set_visible(false);
                isShoot1[i] = 0;
                health--;
            }
        }
        for (int i = 0; i < nBullet2; i++)
        {
            if (Bullet2[i].CollidesWith(sprite))
            {
                Bullet2[i].set_visible(false);
                isShoot2[i] = 0;
                health--;
            }
        }
        return health;
    }
	
	public void setCondition()
    {
        int iX = (int)Enemy.getX();
        int iY = (int)Enemy.getY();
        if (health <= 0)
        {
            Enemy.set_visible(false);
            health = 2;
            isDeath = 2;
            checkDeath = true;
            Random ran = new Random();
            havePower = ran.nextInt(4);
//            death.setPosition(iX, iY);
//            death.setVisible(true);
            if (havePower == 1)
            {
//                power.setPosition(iX, iY);
//                power.setVisible(true);
            }
            //sound.enemyDeathSound();
        }
        if (isDeath >= 0)
        {
            //death.NextFrame();
            isDeath--;
        }
        else
        {
            //death.setVisible(false);
        }
    }
    public int getHealth()
    {
        return health;
    }
    public void setHealth(int health)
    {
        this.health = health;
    }
    public int checkPower(Sprite2D sprite, int pow, int scrY)
    {
//        if (power.CollidesWith(sprite))
//        {
//            power.setVisible(false);
//            pow++;
//        }
//        if (power.getY() > 0 + iheight)
//        {
//            power.setVisible(false);
//        }
        return pow;
    }
    //di nguoc len
    public void moveEnemyType1(int scrY)
    {
        if (Enemy.getY() > 0 + 15)
        {
            //Enemy.Move(0, 1);
        }
        //Enemy.NextFrame();
    }
    //di xuong
    public void moveEnemyType2(int scrY)
    {
        Enemy.Move(0, 3);
        //Enemy.NextFrame();
        if (Enemy.getY() - Enemy.get_height() / 2 > 0 + iHeight)
        {
            Enemy.set_visible(false);
        }
    }
    public void setPosition(int x, int y)
    {
        Enemy.set_visible(true);
        checkDeath = false;
        Enemy.setPosition(x, y);
    }
    public Sprite2D getEnemy()
    {
        return Enemy;
    }
    public void refreshEnemy()
    {
        Enemy.set_visible(false);
//        power.setVisible(false);
//        death.setVisible(false);
        for (int i = 0; i < nBullet1; i++)
        {
            Bullet1[i].set_visible(false);
        }
        for (int i = 0; i < nBullet2; i++)
        {
            Bullet2[i].set_visible(false);
        }
    }
}
